﻿
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_SMS_CheckUpdate]') AND type in (N'P', N'PC'))
    DROP PROCEDURE [dbo].[sproc_SMS_CheckUpdate];
GO
CREATE PROCEDURE [dbo].[sproc_SMS_CheckUpdate]
    @SessionID nvarchar(100),
    @Username nvarchar(255),
    @ActiveNodeID int,
    @ReturnID  int OUTPUT,
    @NewMsgFlag bit OUTPUT
/*

===================================================
功能:
    1.更新activetime         
    2.判断sessionid            
    3.删除5分钟内未活动人
    4.拿到最新在线人数得到在线人数
参数:
    @SessionID  Session码
    @Username  用户名
        
返回值:
     -1 此用户已经不该在线
     =>0 在线人数
===================================================

*/
AS
BEGIN

SET NOCOUNT ON

--判断是否在线
IF NOT EXISTS (SELECT 1 FROM UDS_Online WHERE AUTHKEY=@SessionID and Username=@Username)
BEGIN
    SELECT @ReturnID = -1
END
ELSE 
BEGIN    
    --判断是否有未读新的短消息
    IF NOT EXISTS (
        SELECT 1 FROM UDS_SMS_Msg m, UDS_SMS_Receiver r 
        WHERE
            r.Receiver = @Username 
            and m.ID=r.MsgID 
            and r.IsRead=0
            and m.type=1
    )
        SELECT @NewMsgFlag = 0
    ELSE
        SELECT @NewMsgFlag = 1
        
    --更新在线时间
    UPDATE UDS_Online SET ActiveTime = getdate(), ActiveNodeID = @ActiveNodeID WHERE Username = @Username 

    --删除五分钟钟未活动人员
    DELETE  FROM  UDS_Online WHERE DATEDIFF(n,ActiveTime,getdate())>5

    SELECT @ReturnID=Count(*) FROM UDS_Online
END

END
GO